<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class ShopsController extends AppController{
    var $name = "Shops";
    var $layout = "ht_layout";
    var $uses = array('Shop');



    /*
     * 修改店铺信息初始化
     */
    function editShopinfo(){
        $user_id = $this->Session->read('Auth.User.id');
//        if( empty($user_id) ){
//            $this->redirect("");
//        }
        $params = array();
        $params['conditions'] = array('Shop.user_id'=>2);
        $shopinfo = $this->Shop->find('first',$params);

        if( !empty($shopinfo) ){
            $this->data = $shopinfo;
        }
        //var_dump($shopinfo);die;
        $this->render("editShopinfo");
    }

    /*
     * 更新店铺信息
     */
     function doEditShopinfo(){
        $user_id = $this->Session->read("Auth.User.id");
        //根据user_id获取店铺的id
        $conditions = array();
        $conditions['user_id'] = $user_id;
        $shop_id = $this->Shop->field('id',$conditions);
        if( !$shop_id ){
            $this->redirect("/users/logout");
        }

        //上传图片
        $img_name       = $this->data['Shop']['logo']['name'];
        $img_type       = strtolower($this->data['Shop']['logo']['type']);
        $img_tmp_name   = $this->data['Shop']['logo']['tmp_name'];

//        if( ($img_type!="img/png") && ($img_type!="img/jpg") && ($img_type!="img/jpeg") && ($img_type!="img/gif") ){
//
//        }
        $uploadImgPath  = WWW_ROOT."uploadImg";
        if( !is_dir($uploadImgPath) ){
            mkdir($uploadImgPath);
            chmod($uploadImgPath,'777');
        }

        $currentYear = date("Y");
        if( !is_dir($uploadImgPath.DS.$currentYear) ){
            mkdir($uploadImgPath.DS.$currentYear);
            chmod($uploadImgPath.DS.$currentYear,'777');
        }
        $currentDate = date("d");
        if( !is_dir($uploadImgPath.DS.$currentYear.DS.$currentDate) ){
            mkdir($uploadImgPath.DS.$currentYear.DS.$currentDate);
            chmod($uploadImgPath.DS.$currentYear.DS.$currentDate,'777');
        }

        $newFileName = time().strstr($img_name,'.');
        $newFile = $uploadImgPath.DS.$currentYear.DS.$currentDate.DS.$newFileName;

        if( move_uploaded_file($img_tmp_name,$newFile) ){
           $this->data['Shop']['logo'] = "/uploadImg/".$currentYear."/".$currentDate."/".$newFileName;
        }

        $this->Shop->id = $shop_id;
        $this->data['Shop']['user_id'] = $user_id;
        $this->Shop->save($this->data,false);
        $this->redirect("/shops/editShopinfo");
     }
    
}
?>
